package com.zbkj.crmeb.merchant.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zbkj.crmeb.store.service.HomeStylePreviousService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.zbkj.crmeb.store.dto.HomeStyleDTO;
import com.zbkj.crmeb.store.model.HomeStyle;
import com.zbkj.crmeb.store.service.HomeStyleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 个性化首页
 * @Description: 
 * @author daigb
 * @date 2021-09-25
 */
@RestController
@RequestMapping("api/admin/mer/home/style")
@Api(tags = "个性化首页")
public class MerHomeStyleController extends AdminBaseController {
    @Autowired
    HomeStyleService homeStyleService;
    @Autowired
    HomeStylePreviousService homeStylePreviousService;

    /**
     * 列表
     * @Description:  
     * @author daigb 
     * @param @param type 类型 1: 轮播图 2: 热区图 3: 专区 4: 品牌
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "列表")
    @GetMapping
    public CommonResult<List<HomeStyleDTO>> listEntity(HttpServletRequest request) {
        return CommonResult.success(homeStyleService.listEntity(getMerId(request)));
    }
    /**
     * 列表
     * @Description:
     * @author daigb
     * @param @param type 类型 1: 轮播图 2: 热区图 3: 专区 4: 品牌
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "分页")
    @GetMapping("/page")
    public CommonResult<CommonPage<HomeStyleDTO>> listPage(@ModelAttribute HttpServletRequest request,@ModelAttribute PageParamRequest pageParamRequest) {
        //CommonPage<HomeStyleDTO> listPage = CommonPage.restPage(homeStyleService.listPage(getMerId(request), pageParamRequest));

        return null;//CommonResult.success(listPage);
    }

    /**
     * 通过类型查询
     * @Description:  
     * @author daigb 
     * @param @param type 类型 1: 轮播图 2: 热区图 3: 专区 4: 品牌
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "通过类型获取")
    @GetMapping("/{type}")
    public CommonResult<List<HomeStyleDTO>> getEntity(@ApiParam(value = "类型", required = true) @PathVariable("type") Integer type, HttpServletRequest request) {
        return CommonResult.success(homeStyleService.listEntityByType(getMerId(request), type));
    }
    
    /**
     * 新增
     * @Description:  
     * @author daigb 
     * @param @param homeStyle
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping
    public CommonResult<Object> save(@RequestBody List<HomeStyleDTO> homeStyleDTOs, HttpServletRequest request) {
        return CommonResult.success(homeStyleService.saveEntity(getMerId(request), homeStyleDTOs));
    }
    
    /**
     * 启用或禁用
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @param isShow
     * @param @param request
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @PutMapping("/is-enabled/{id}/update/{isShow}")
    @ApiOperation("禁用或启用")
    public CommonResult<Object> enabled(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, @ApiParam(value = "启用状态 1启用 0 禁用", required = true) @PathVariable("isShow") Integer isShow, HttpServletRequest request) {
        return CommonResult.success(homeStyleService.update(Wrappers.<HomeStyle>lambdaUpdate()
                .eq(HomeStyle :: getId, id)
                .eq(HomeStyle :: getMerId, getMerId(request))
                .set(HomeStyle :: getIsShow, isShow)
                ));
    }
    
    /**
     * 根据id删除
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, HttpServletRequest request) {
        HomeStyle homeStyleDB = homeStyleService.getOne(Wrappers.<HomeStyle>lambdaQuery()
                .eq(HomeStyle :: getMerId, getMerId(request))
                .eq(HomeStyle :: getId, id)
                .last("LIMIT 1")
                );
        if(null == homeStyleDB) {
            return CommonResult.validateFailed("记录不存在");
        }
        
        return CommonResult.success(homeStyleService.removeById(id));
    }

    /**
     * 功能描述: 根据商户id备份之前的模板供商户还原
     * @Param: [save, request]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Object>
     * @Author: wangzh
     * @Date: 2022/9/15 9:52
     */
    @ApiOperation(value = "根据商户id备份")
    @PostMapping("/previous")
    public CommonResult<Object> savePrevious(Boolean save ,HttpServletRequest request){
        return CommonResult.success(homeStylePreviousService.savePrevious(save, getMerId(request)));
    }

    /**
     * 功能描述: 根据商户id还原之前的模板，并备份当前的模板
     * @Param: [request]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Object>
     * @Author: wangzh
     * @Date: 2022/9/15 10:57
     */
    @ApiOperation(value = "根据商户id还原之前的模板")
    @PostMapping("/restore")
    public CommonResult<Object> restoreStyle(HttpServletRequest request){
        return CommonResult.success(homeStylePreviousService.restoreStyle(getMerId(request)));
    }

    /**
     * 功能描述: 活动管理设置小程序首页模板时，使用提供的模板时进行调用，删除之前的设置的模板
     * @Param: [activityId]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Boolean>
     * @Author: wangzh
     * @Date: 2022/12/15 17:55
     */
    @ApiOperation(value = "删除活动管理之前的首页配置模板")
    @DeleteMapping("/activity/{activityId}")
    public CommonResult<Boolean> deleteActivityStyle(@PathVariable Integer activityId){
        return CommonResult.success(homeStyleService.deleteActivityStyle(activityId));
    }
}
